package com.seek.quartz;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.seek.service.LoanService;
import com.seek.util.CommonUtil;
import com.seek.util.Push2SingleUtil;
import com.sinovatech.common.config.GlobalConfig;

/**
 * 逾期未还款客户预警消息推送
 * @author lushuangteng
 *
 */
public class LoanPayWarningQuartz {
	private static Logger logger = LoggerFactory.getLogger(OperStatsCountQuartz.class);
	@Resource
	private LoanService loanService;
	public static String DAYS = GlobalConfig.getProperty("main","OVERTIME_LOANWARNING_DAYS");
	@Resource
	Push2SingleUtil push2SingleUtil;

	@SuppressWarnings("unchecked")
	public void run(){
		try{
			// 取得逾期未还款且逾期天数在N天之内的贷款, 给对应的客户发送还款提醒:如果一个客户有多笔逾期记录只预警一次
			StringBuffer sb = new StringBuffer("select a.client_id from t_customer a where ");
			sb.append(" exists (select 1 from t_loan b where a.id=b.customer_id and b.loan_status in (3,6) and datediff(now(),b.end_time)>=0 and datediff(now(),b.end_time)<?)");
			List<Map<String,Object>> data = loanService.findMapBySql(sb.toString(),new Object[]{DAYS});
			if (data == null || data.size() < 1)
				return;
			for (Map<String,Object> m : data){
				String clientId = m.get("client_id")==null ? null : m.get("client_id").toString();
				if (CommonUtil.isEmpty(clientId))
					continue;
				push2SingleUtil.push2Single(Push2SingleUtil.TYPE_OF_REPAY_WARNING, clientId);
			}
		}catch(Exception e){
			logger.error(e.getMessage());
		}
	}
}
